package org.eenie.learn.seedquartz.startup;

import io.reactivex.observers.DefaultObserver;
import okhttp3.ResponseBody;
import org.eenie.learn.seedquartz.config.Config;
import org.eenie.learn.seedquartz.controllers.TicketController;
import org.eenie.learn.seedquartz.quartz.job.PayNoticeJob;
import org.eenie.learn.seedquartz.quartz.job.SubmitJob;
import org.eenie.learn.seedquartz.quartz.job.TicketJob;
import org.eenie.learn.seedquartz.quartz.manage.QuartzManager;
import org.eenie.learn.seedquartz.utils.OrderInfo;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;


@Configuration
public class AppReadyListener implements ApplicationListener<ContextRefreshedEvent> {

    private final static Logger logger = LoggerFactory.getLogger(QuartzManager.class);
    @Autowired
    private ApplicationArguments applicationArguments;


    @Autowired
    QuartzManager mQuartzManager;

    @Autowired
    TicketController mTicketController;


    @Autowired
    OrderInfo mOrderInfo;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("app is running " + applicationArguments.getNonOptionArgs());

        for (String arg : applicationArguments.getNonOptionArgs()) {
            String[] m = arg.split("=");
            if ("zhhxuinfo".equals(m[0])) {
                Config.zhhxuinfo = m[1];
            } else if ("sessionId".equals(m[0])) {
                Config.sessionId = m[1];
            } else if ("key".equals(m[0])) {
                Config.key = m[1];
            } else if ("type".equals(m[0])){
                Config.type = Integer.parseInt(m[1]);
            }
        }

        mOrderInfo.refresh();
        mTicketController.refresh();

//        try {
//            TicketJob.JOB_ID = mQuartzManager.createTask(TicketJob.class, "listening", "0/20 * * * * ?");
//            PayNoticeJob.JOB_ID = mQuartzManager.createTask(PayNoticeJob.class, "pay", "0/50 * * * * ?");
//            SubmitJob.JOB_ID = mQuartzManager.createTask(SubmitJob.class, "submit", "0/3 * * * * ?");
//            mQuartzManager.resumeTask(TicketJob.JOB_ID);
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
        mTicketController.setTargetTicket().subscribe(new DefaultObserver<ResponseBody>() {
            @Override
            public void onNext(ResponseBody jsonObject) {
                try {
                    TicketJob.JOB_ID = mQuartzManager.createTask(TicketJob.class, "listening", "0/20 * * * * ?");
                    PayNoticeJob.JOB_ID = mQuartzManager.createTask(PayNoticeJob.class, "pay", "0/50 * * * * ?");
                    SubmitJob.JOB_ID = mQuartzManager.createTask(SubmitJob.class, "submit", "0/3 * * * * ?");
                    mQuartzManager.resumeTask(TicketJob.JOB_ID);
                } catch (SchedulerException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onComplete() {

            }
        });


    }
}
